<template>
  <div>
    <a-card>
      <div class="flex justify-between items-center">
        <a-button type="primary" class="mr-4" @click="router.go(-1)"> 返回 </a-button>
        <a-descriptions class="mt-3" :column="4">
          <a-descriptions-item label="单号"> {{ formState?.lsh || '-' }}</a-descriptions-item>
          <a-descriptions-item label="入库日期"> {{ formState?.inDate || '-' }}</a-descriptions-item>
          <a-descriptions-item label="制单人">{{ formState?.createUser || '-' }}</a-descriptions-item>
        </a-descriptions>
      </div>
    </a-card>
    <a-card class="mt-2">
      <div class="flex">
        <a-button class="mb-2" type="primary" @click="onAdd"> 新增 </a-button>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" column-code="000601" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <BaseButton confirm-text="是否删除?" type="text" danger size="small" @click="onClickDelete(record)"> 删除 </BaseButton>
              <a-button type="link" size="small"> 打印 </a-button>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <AddInfo v-model:open="editOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref, watchEffect } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import BaseButton from '@/components/BaseButton'
import AddInfo from './AddInfo'

const editOpen = ref(false)
const rowData = ref({})
const router = useRouter()
const route = useRoute()
const formState = ref({})

const { loading, tableData, pagination, getData } = useTable({
  url: '/bloodm/bldStoreInRecord/getBldStoreDetailList',
  immediate: true,
  formatParams: () => {
    return {
      lsh: route.query?.lsh
    }
  }
})

// 获取信息
const getRecordList = async () => {
  const { code, data, msg } = await post('/bloodm/bldStoreInRecord/getBldStoreInRecordList', { lsh: route.query?.lsh })
  if (code !== 0) return message.error(msg)
  if (data.length > 0) {
    formState.value = data[0]
  }
}

// 新增
const onAdd = () => {
  rowData.value = {}
  editOpen.value = true
}

// 删除
const onClickDelete = async (row) => {
  const { code, msg } = await post('/bloodm/bldStoreInRecord/doDelBldStoreDetail', { id: row.id })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}

// const onPrint = async (row) => {
//   const { code, msg, data } = await post('/drugStorage/psiform/printWarehousingList', { rkdh: row.rkdh })
//   if (code !== 0) return message.error(msg)
//   window.openExe?.rpt(data) //调用winform打印
// }

watchEffect(() => {
  if (route.query?.lsh) {
    getRecordList()
  }
})
</script>
